package org.colorfoo;

import okhttp3.*;

public class RequestTest {

    private final OkHttpClient httpClient = new OkHttpClient();
    public static void main(String[] args) throws Exception {
        RequestTest im = new RequestTest();
        im.commonUpdateConfig("{\"userConfig\":{\"imei\":\"358800011050029\",\"fallDown\":1},\"sendBody\":{\"cmd\":\"BP76\",\"imei\":\"358800011050029\",\"factory\":\"TW-C1\"}}");
    }
    public void commonUpdateConfig(String data) throws Exception {
        String url = getCommonUpdateConfigUrl();
        Headers headers = getHeaders();

        try {
            RequestBody body = RequestBody.create(data, MediaType.parse("application/json; charset=utf-8"));

            Request request = new Request.Builder()
                    .url(url)
                    .headers(headers)
                    .post(body)
                    .build();

            Response response = httpClient.newCall(request).execute();
            if (!response.isSuccessful()) {
                throw new Exception(String.format("request colofoo openapi fail, %s", response.message()));
            }

            String respData = response.message();
        } catch (Exception e) {
        }
    }


    private String getCommonUpdateConfigUrl() {
        return String.format("%s/device/commonUpdateConfig", "https://api-xintai.colofoo.com/openapi");
    }

    private Headers getHeaders() {
        return new Headers.Builder()
                .add("appId", "zvcE8ohF")
                .add("appSecret", "4550cb727a9825b1f6127481d5bf6894bfc8c45c")
                .build();
    }
}
